package cl.uchile.sketchpad.persistance.model;

import static javax.persistence.FetchType.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "user_")
@JsonIgnoreProperties({"subjects","subjectVersions"})
public class User implements Serializable {
	
	public static enum Type {
		STUDENT("student"), TEACHER("teacher"), ADMIN("admin"), MONITOR("tv");
		
		private String value;
		
		private Type(String value) {
			this.value = value;
		}
		
		public String getValue() {
			return this.value;
		}
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1742585504045980674L;
	private Integer id;
	private String username;
	private String name;
	private String lastname;
	private String type;
	private String mail;

	private List<Subject> subjects = new ArrayList<Subject>(0);
	private List<SubjectVersion> subjectVersions = new ArrayList<SubjectVersion>(0);

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	@ManyToMany(fetch = LAZY)
	@JoinTable(name = "subject_teacher",
			joinColumns = { @JoinColumn(name = "user_id") },
			inverseJoinColumns = { @JoinColumn(name = "subject_id") })
	@OrderBy("name")
	public List<Subject> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<Subject> subjects) {
		this.subjects = subjects;
	}

	@ManyToMany(fetch = LAZY, cascade = CascadeType.ALL)
	@JoinTable(name="subject_version_user",
	    joinColumns = { @JoinColumn(name="user_id") },
	    inverseJoinColumns = { @JoinColumn(name="subject_version_id") } )
	@OrderBy("name")
	public List<SubjectVersion> getSubjectVersions() {
		return subjectVersions;
	}

	public void setSubjectVersions(List<SubjectVersion> subjectVersions) {
		this.subjectVersions = subjectVersions;
	}
	
}
